﻿using System;
using Microsoft.Practices.Unity;
using Rhino.Mocks;

namespace TestByWire.Factories.StubbingStrategies
{
    internal class StubbingStrategy : StubStrategyBase
    {
        private PipelineSettings _pipelineSettings;
        private bool _completesBuild;

        public StubbingStrategy(IUnityContainer container)
        {
            _pipelineSettings = container.Resolve<PipelineSettings>();
        }

        public override bool RegisterOnContainer
        {
            get { return true; }
        }

        public override bool CompletesBuild
        {
            get
            {
                return _completesBuild;
            }
        }

        public override bool CanHandleType(Type type, string instanceName)
        {
            return !type.IsValueType && !type.IsSealed && !_pipelineSettings.DoNotStubList.Contains(new DoNotStub(type, instanceName));
        }

        protected override object TryCreateInstance(Type type)
        {
            _completesBuild = type.IsAbstract && !type.IsInterface;

            return MockRepository.GenerateStub(type);
        }
    }
}
